package com.gitee.wsl.common.ui.symbol

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.unit.LayoutDirection
import kotlin.math.PI
import kotlin.math.sin

fun Path.wavyLine(
    wavy: Wavy,
    layoutDirection:LayoutDirection = LayoutDirection.Ltr,
    size:Rect,
    incremental: Boolean = false
){
    with(wavy){
        val startRadians = waveSpread * (waveShift) / waveLength * (2 * PI)
        val startHeightFactor = if (incremental) 0f else 1f
        val startY = (sin(startRadians) * startHeightFactor * waveHeight + size.height) / 2
        moveTo(startOffset.x, startY.toFloat())
        val range = if (layoutDirection == LayoutDirection.Rtl) {
            startOffset.x.toInt() downTo valueOffset.x.toInt()
        } else {
            startOffset.x.toInt()..valueOffset.x.toInt()
        }
        for (x in range) {
            val heightFactor = if (incremental) (x - range.first).toFloat() / (range.last - range.first) else 1f
            val radians = waveSpread * (x - range.first + waveShift) / waveLength * (2 * PI)
            val y = (sin(radians) * heightFactor * waveHeight + size.height) / 2
            lineTo(x.toFloat(), y.toFloat())
        }
    }
}

class Wavy(var startOffset: Offset,
           var valueOffset: Offset,
           var waveLength: Float,
           var waveHeight: Float,
           var waveSpread: Float,
           var waveShift: Float):Symbol